using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using GonzalesCore.CoreElements;

using Kypris2.Utils;
using Kypris2.CoreElements;
using Kypris2.CoreManagers;

using Jamila2.CoreElements;
using Jamila2.Database;
using Jamila2.Tools;

namespace GonzalesCore.CoreManagers
{
    #region RawDataManagerBase

    public abstract class RawDataManagerBase : KyprisManagerBase, IManager, IDisposable
    {        
        protected PersonalSessionAccount CurrentModuleSession = null;
        protected LogManager CurrentLogManager = null;
        protected ArrayList SQLList = new ArrayList();

        public int TotalInTheList
        {
            get
            {
                return this.SQLList.Count;
            }
        }

        public RawDataManagerBase(PersonalSessionAccount PrivateConfig)
            : base(PrivateConfig)
        {
            this.CurrentModuleSession = PrivateConfig;
            this.CurrentLogManager = new LogManager(PrivateConfig);
        }

        protected abstract void AddData(object[] Parameters);
        public abstract bool ExecutePreInjectAction();        
        public abstract bool ExecuteFinalAction();

        public abstract bool ExecuteDataInject();
        public virtual bool ExecuteDataInject(string ErrorMessageHeader)
        {                    
            bool smooth = true;

            if (this.TryConnection())
            {
                smooth = this.CurDBEngine.ExecuteQuery(this.SQLList);
                if (!smooth)
                {
                    this.ErrMsg = ErrorMessageHeader + " : Failed at this.CurDBEngine.ExecuteQuery() : " + this.CurDBEngine.ErrorMessage;
                }
            }
            else
            {
                this.ErrMsg = ErrorMessageHeader + " : Exception : " + this.ErrMsg;
            }

            return smooth;
        }
    }

    #endregion

    #region RawEmployeeDataManager

    public class RawEmployeeDataManager : RawDataManagerBase
    {
        private GonzalesDataStructure DataStructrure = new GonzalesDataStructure();
        private string EntityID = "";

        public RawEmployeeDataManager(PersonalSessionAccount PrivateConfig)
            : base(PrivateConfig)
        {
            
        }

        public void AddData(string EntityID, string CountryCode, string CompanyCode, string EmployeeID, string EmployeeName, string EmployeeLocal, string EmailID, string CostCenter, string Email, string VendorID)
        {
            object[] DataObj = new object[9];
            DataObj[0] = CountryCode;
            DataObj[1] = CompanyCode;
            DataObj[2] = EmployeeID;
            DataObj[3] = EmployeeName;
            DataObj[4] = EmployeeLocal;
            DataObj[5] = EmailID;
            DataObj[6] = CostCenter;
            DataObj[7] = Email;
            DataObj[8] = VendorID;

            this.EntityID = EntityID;

            this.AddData(DataObj);
        }

        protected override void AddData(object[] Parameters)
        {
            DatabaseParameters ValParameters = new DatabaseParameters();
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.TempEmployee.CountryCode.ActualFieldName, Parameters[0].ToString()));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.TempEmployee.CompanyCode.ActualFieldName, Parameters[1].ToString()));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.TempEmployee.EmployeeID.ActualFieldName, Parameters[2].ToString()));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.TempEmployee.EmployeeName.ActualFieldName, Parameters[3].ToString().Replace("'", "''")));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.TempEmployee.EmployeeLocalName.ActualFieldName, Parameters[4].ToString().Replace("'", "''"), true, true));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.TempEmployee.EmailID.ActualFieldName, Parameters[5].ToString().Replace("'","''")));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.TempEmployee.CostCenter.ActualFieldName, Parameters[6].ToString()));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.TempEmployee.Email.ActualFieldName, Parameters[7].ToString().Replace("'","''")));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.TempEmployee.VendorID.ActualFieldName, Parameters[8].ToString()));

            this.CurSQLFactory.InsertCommand(ValParameters, this.DataStructrure.Tables.TempEmployee.ActualTableName);
            this.SQLList.Add(this.CurSQLFactory.SQL);
        }

        public override bool ExecutePreInjectAction()
        {
            // clear employee table
            this.CurSQLFactory.DeleteCommand(null, this.DataStructrure.Tables.TempEmployee.ActualTableName);
            this.SQLList.Add(this.CurSQLFactory.SQL);

            return true;
        }

        public override bool ExecuteDataInject()
        {
            return base.ExecuteDataInject("[RawEmployeeDataManager.ExecuteDataInject]");
        }

        public override bool ExecuteFinalAction()
        {
            DatabaseParameters KeyParameters = new DatabaseParameters();
            KeyParameters.Add (new DatabaseParameter(this.DataStructrure.StoredProcedures.TransferEmployee.Param_EntityID.ActualFieldName, this.EntityID));

            this.CurSQLFactory.ExecuteStoredProcedure(this.DataStructrure.StoredProcedures.TransferEmployee.ActualTableName, KeyParameters);
            this.SQLList.Add(this.CurSQLFactory.SQL);

            return true;
        }

    }

    #endregion

    #region RawProjectManager

    public class RawProjectManager : RawDataManagerBase
    {
        private GonzalesDataStructure DataStructrure = new GonzalesDataStructure();

        public RawProjectManager(PersonalSessionAccount PrivateConfig)
            : base(PrivateConfig)
        {

        }

        public void AddData(string EntityCode, string ProjectID, string Description, string ProjectType)
        {
            object[] DataObj = new object[4];
            DataObj[0] = EntityCode;
            DataObj[1] = ProjectID;
            DataObj[2] = Description;
            DataObj[3] = ProjectType;

            this.AddData(DataObj);
        }

        protected override void AddData(object[] Parameters)
        {
            DatabaseParameters ValParameters = new DatabaseParameters();
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.TempProject.EntityID.ActualFieldName, Parameters[0].ToString()));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.TempProject.ProjectID.ActualFieldName, Parameters[1].ToString()));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.TempProject.ProjectDescription.ActualFieldName, Parameters[2].ToString().Replace("'", "''"), true, true));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.TempProject.ProjectType.ActualFieldName, Parameters[3].ToString()));

            this.CurSQLFactory.InsertCommand(ValParameters, this.DataStructrure.Tables.TempProject.ActualTableName);
            this.SQLList.Add(this.CurSQLFactory.SQL);
        }

        public override bool ExecutePreInjectAction()
        {
            // clear employee table
            this.CurSQLFactory.DeleteCommand(null, this.DataStructrure.Tables.TempProject.ActualTableName);
            this.SQLList.Add(this.CurSQLFactory.SQL);

            return true;
        }

        public override bool ExecuteDataInject()
        {
            return base.ExecuteDataInject("[RawProjectManager.ExecuteDataInject]");
        }

        public override bool ExecuteFinalAction()
        {
            this.CurSQLFactory.ExecuteStoredProcedure(this.DataStructrure.StoredProcedures.TransferProject.ActualTableName, null);
            this.SQLList.Add(this.CurSQLFactory.SQL);

            return true;
        }

    }

    #endregion

    #region RawExpenseDetailManager

    public class RawExpenseDetailManager : RawDataManagerBase
    {
        private GonzalesDataStructure DataStructrure = new GonzalesDataStructure();

        public RawExpenseDetailManager(PersonalSessionAccount PrivateConfig)
            : base(PrivateConfig)
        {

        }

        public void AddData(string EntityCode, string AccountCode, string Description)
        {
            object[] DataObj = new object[3];
            DataObj[0] = EntityCode;
            DataObj[1] = AccountCode;
            DataObj[2] = Description;            

            this.AddData(DataObj);
        }

        protected override void AddData(object[] Parameters)
        {
            DatabaseParameters ValParameters = new DatabaseParameters();
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.TempExpenseDetail.EntityID.ActualFieldName, Parameters[0].ToString()));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.TempExpenseDetail.ExpenseDetailCode.ActualFieldName, Parameters[1].ToString()));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.TempExpenseDetail.Description.ActualFieldName, Parameters[2].ToString().Replace("'", "''"), true, true));

            this.CurSQLFactory.InsertCommand(ValParameters, this.DataStructrure.Tables.TempExpenseDetail.ActualTableName);
            this.SQLList.Add(this.CurSQLFactory.SQL);
        }

        public override bool ExecutePreInjectAction()
        {
            // clear employee table
            this.CurSQLFactory.DeleteCommand(null, this.DataStructrure.Tables.TempExpenseDetail.ActualTableName);
            this.SQLList.Add(this.CurSQLFactory.SQL);

            return true;
        }

        public override bool ExecuteDataInject()
        {
            return base.ExecuteDataInject("[RawExpenseDetailManager.ExecuteDataInject]");
        }

        public override bool ExecuteFinalAction()
        {
            this.CurSQLFactory.ExecuteStoredProcedure(this.DataStructrure.StoredProcedures.TransferExpenseDetail.ActualTableName, null);
            this.SQLList.Add(this.CurSQLFactory.SQL);

            this.CurSQLFactory.ExecuteStoredProcedure(this.DataStructrure.StoredProcedures.SyncExpenseDetailInLocalLanguage.ActualTableName, null);
            this.SQLList.Add(this.CurSQLFactory.SQL);

            return true;
        }

    }

    #endregion

    #region RawExRateDataManager

    public class RawExRateDataManager : RawDataManagerBase
    {
        private GonzalesDataStructure DataStructrure = new GonzalesDataStructure();

        public RawExRateDataManager(PersonalSessionAccount PrivateConfig)
            : base(PrivateConfig)
        {

        }

        public void AddData(string SourceCurrency, string TargetCurrency, decimal ExRate)
        {
            object[] DataObj = new object[3];
            DataObj[0] = SourceCurrency;
            DataObj[1] = TargetCurrency;
            DataObj[2] = ExRate;

            this.AddData(DataObj);
        }

        protected override void AddData(object[] Parameters)
        {
            DatabaseParameters ValParameters = new DatabaseParameters();
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.TempExchangeRate.SourceCurrency.ActualFieldName, Parameters[0].ToString()));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.TempExchangeRate.TargetCurrency.ActualFieldName, Parameters[1].ToString()));
            ValParameters.Add(new DatabaseParameter(this.DataStructrure.Tables.TempExchangeRate.ExRate.ActualFieldName, Parameters[2].ToString()));

            this.CurSQLFactory.InsertCommand(ValParameters, this.DataStructrure.Tables.TempExchangeRate.ActualTableName);
            this.SQLList.Add(this.CurSQLFactory.SQL);
        }

        public override bool ExecutePreInjectAction()
        {
            // clear temp table
            this.CurSQLFactory.DeleteCommand(null, this.DataStructrure.Tables.TempExchangeRate.ActualTableName);
            this.SQLList.Add(this.CurSQLFactory.SQL);


            return true;
        }

        public override bool ExecuteDataInject()
        {
            return base.ExecuteDataInject("[RawExRateDataManager.ExecuteDataInject]");
        }

        public override bool ExecuteFinalAction()
        {
            this.CurSQLFactory.ExecuteStoredProcedure(this.DataStructrure.StoredProcedures.TransferExchangeRate.ActualTableName, null);
            this.SQLList.Add(this.CurSQLFactory.SQL);

            return true;
        }

    }

    #endregion
}
